/**
 * Created by zhang on 2016/1/20.
 */
var ige = angular.module("IGE", ['ui.router', 'ngAnimate', 'ngMaterial', 'ngMessages', 'ngCookies']);
ige.systemSetting = {
    apiDomain: 'http://192.168.12.117:83/',
    defaultPageSize: 20,
    batchDataSize: 2000
};
ige.config(['$stateProvider', '$urlRouterProvider', '$mdThemingProvider', '$httpProvider',
    function ($stateProvider, $urlRouterProvider, $mdThemingProvider, $httpProvider) {
        $httpProvider.defaults.xsrfHeaderName = '_csrf',
            $httpProvider.defaults.xsrfCookieName = '_csrf',
            $httpProvider.defaults.cache = true,
            $mdThemingProvider.theme('default')
                .primaryPalette('green')
                .accentPalette('pink')
                .warnPalette('red');
        $urlRouterProvider.when("", "/Login");
        $stateProvider
            .state("MainNav", {
                url: "/MainNav",
                templateUrl: "Share/_nav.html",
                controller: 'MainNavController'
            })
            .state("MainNav.Department", {
                url: "/Department",
                templateUrl: "Department/_department.html",
                controller: 'DepartmentController'
            })
            .state("MainNav.AddDepartment", {
                url: "/AddDepartment",
                templateUrl: "Department/_add_department.html",
                controller: 'AddDepartmentController'
            }).state("MainNav.EditDepartment", {
                url: "/EditDepartment/:id/:name/:desc",
                templateUrl: "Department/_edit_department.html",
                controller: 'EditDepartmentController'
            })
            .state("Login", {
                url: "/Login",
                templateUrl: "Account/_login.html",
                controller: 'LoginController'
            })
            .state("MainNav.AddSystemUser", {
                url: "/AddSystemUser",
                templateUrl: "Account/_add_system_user.html",
                controller: 'AddSystemUserController'
            })
            .state("MainNav.EditSystemUser", {
                url: "/EditSystemUser/:id",
                templateUrl: "Account/_edit_system_user.html",
                controller: 'EditSystemUserController'
            })
            .state("MainNav.SystemUser", {
                url: "/SystemUser",
                templateUrl: "Account/_system_user.html",
                controller: 'SystemUserController'
            }).state("MainNav.AddEmployee", {
                url: "/AddEmployee",
                templateUrl: "Employee/_add_employee.html",
                controller: 'AddEmployeeController'
            })
            .state("MainNav.EditEmployee", {
                url: "/EditEmployee/:id",
                templateUrl: "Employee/_edit_employee.html",
                controller: 'EditEmployeeController'
            })
            .state("MainNav.Employee", {
                url: "/Employee",
                templateUrl: "Employee/_employee.html",
                controller: 'EmployeeController'
            })
            .state("MainNav.DetailEmployee", {
                url: "/DetailEmployee/:id",
                templateUrl: "Employee/_detail_employee.html",
                controller: 'DetailEmployeeController'
            }).state("MainNav.EmployeeReward", {
                url: "/EmployeeReward/:methodid/:resultid/:rewardAvgId/:ids",
                templateUrl: "Employee/_rewards_employee.html",
                controller: 'EmployeeRewardController'
            })
            .state("MainNav.TaxSetting", {
                url: "/TaxSetting",
                templateUrl: "TaxSetting/_tax_setting.html",
                controller: 'TaxSettingController'
            })
            .state("MainNav.EditTaxSetting", {
                url: "/EditTaxSetting/:id",
                templateUrl: "TaxSetting/_edit_tax_setting.html",
                controller: 'EditTaxSettingController'
            })
            .state("MainNav.AddTaxSetting", {
                url: "/AddTaxSetting",
                templateUrl: "TaxSetting/_add_tax_setting.html",
                controller: 'AddTaxSettingController'
            });

    }]);

ige.factory("EventBus", function () {
    var eventMap = {};

    var EventBus = {
        on: function (eventType, handler) {
            //multiple event listener
            if (!eventMap[eventType]) {
                eventMap[eventType] = [];
            }
            eventMap[eventType].push(handler);
        },

        off: function (eventType, handler) {
            if (!handler) {
                eventMap[eventType] = null;
            } else {
                for (var i = 0; i < eventMap[eventType].length; i++) {
                    if (eventMap[eventType][i] === handler) {
                        eventMap[eventType].splice(i, 1);
                        break;
                    }
                }
            }
        },

        fire: function (event) {
            var eventType = event.type;
            if (eventMap && eventMap[eventType]) {
                for (var i = 0; i < eventMap[eventType].length; i++) {
                    eventMap[eventType][i](event);
                }
            }
        }
    };
    return EventBus;

    //注意，如果在复杂的应用中使用事件总线，需要慎重规划事件名，推荐使用业务路径，比如："portal.menu.selectedMenuChange"，以避免事件冲突。
    //EventBus.on("someEvent", function(event) {
    //    // 这里处理事件
    //    var c = event.data.a + event.data.b;
    //});

    //EventBus.fire({
    //    type: "someEvent",
    //    data: {
    //        aaa: 1,
    //        bbb: 2
    //    }
    //});
});

ige.directive('elheightresize', ['$window', function ($window) {
    return {
        link: function (scope, elem, attrs) {
            scope.onResize = function () {
                var header = document.getElementsByTagName('header')[0];
                $(elem).height($window.document.body.clientHeight);
            };
            scope.onResize();

            angular.element($window).bind('resize', function () {
                scope.onResize();
            })
        }
    }
}]);